<template>
    <div class="tree"> 
        <div>
          <el-row  >
            <el-col :span="16">
              <el-input v-model="filterText" placeholder="输入名称搜索"  clearable></el-input>  
               </el-col>
            <el-col :span="8">
              <el-button type="primary" @click="reloadTree">刷新</el-button> 
              </el-col>
          </el-row>
        </div>
        
        <el-tree
            class="filter-tree"
            :data="treeData"
            :props="props"
            :load="loadNode"
            ref="treeRef"
            :key="casKey"
            :highlight-current="true"
            :filter-node-method="filterNode"
            lazy
            @node-click="handleNodeClick"
            
        >
          <span class="custom-tree-node" slot-scope="{ data }">
            <span>
              <i v-if="data.type==2" class="el-icon-unlock"></i>
              <i v-else-if="data.type==1" class="el-icon-tickets"></i>
              {{ data.name }}
            </span>
          </span>

        </el-tree>
  </div>
</template>


<script>
// import qs from 'qs'
export default {
    props:{
        jarPath: String
    },
  data() {
    return {
      treeData:[],
      filterText:'',
      props: {
        label: "name",
        children: "children",
        isLeaf: 'isLeaf'
      },
      casKey: true,

      listVM: [],
      listClass: [],
      classCode:'',
      methodList:[],
    };
  },
   watch: {
      filterText(val) {
        this.$refs.treeRef.filter(val);
      }
    },
  mounted() {
    // this.getlistVm();
    // console.log(this.jarPath)
    //  this.getlistClass(this.jarpath,'')
  },
  methods: {
    filterNode(value, data) {
      console.log(data)
        if (!value) return true;
        return data.name.indexOf(value) !== -1;
      },
    handleNodeClick(data) {
      // console.log(data);
      this.$emit("showClassCode",data)
    },
    // 加载节点
    loadNode(node, resolve) {
      // console.log(node);
    //   var self = this;
      if (node.level === 0) {
        return resolve([{ name: "类列表" }]);
      }

      var hasChild;
   

        // 获取类
    //   if (node.level === 2) {
        if (node.data.name === "类列表"){
            this.getlistClass(this.jarPath,'')
            hasChild= true;
            setTimeout(() => {
                var data;
                if (hasChild) {
                    data = this.listClass;
                } else {
                data = [];
                }
                resolve(data);
            }, 1000);
      }

        // 获取方法
      if (node.level === 2){
        console.log(22) 
        let name = node.data.name;
        let packageName = node.data.packageName;

        this.getMethodList(packageName,name)
        hasChild= true;
        setTimeout(() => {
            var data;
            console.log(555555);
            // console.log(this.methodList);
            if (hasChild) {
                data = this.methodList;
            } else {
            data = [];
            }
            resolve(data);
        }, 1000);
      }

    },
    reloadTree(){
       const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.5)'
        });
        this.casKey =  !this.casKey;

        setTimeout(() => {
          loading.close();
        }, 200);
      
      
    },

    getlistVm() {
      var that = this;
      this.$axios.get("/bytespy/api/v1/listVm",{}).then((data) => {
            if(data.code=='0'){
                 var todata = [];
                data.data.forEach((item) => {
                //  console.log(item)
                let tmp = {};
                tmp.name = item.displayName;
                tmp.id = item.id;
                todata.push(tmp);
                });
                that.listVM = todata;
                console.log(that.listVM);
            }else{
                that.listVM=[]
            }
        
      }).catch(err=>{
          console.log(err)
      });
    },
    getlistClass(jarpath,prefix) {
      var that = this;
      this.listClass=[];
      // jarpath = 'E:\\java_agent_demo-master\\main\\target\\main-1.0-SNAPSHOT.jar';
      this.$axios.get('/bytespy/api/v1/listClass',{ params: {
           jarPath: jarpath,
           prefix: prefix
      } }).then((data) => {
            if(data.code=='0'){
                var todata = [];
                data.data.forEach((item) => {
                    //  console.log(item)
                    let tmp = {}
                    tmp.type=1 
                    tmp.name = item.className
                    tmp.className = item.className
                    tmp.packageName = item.packageName
                    // tmp.isLeaf= true
                    todata.push(tmp);
                });
                that.listClass = todata;
                // console.log(that.listClass);
            }else{
                that.listClass=[]
            }
        }).catch((error)=>{
          console.log(error)
          
      });
    },
    
    getMethodList(packageName,className){
        var that = this;
        this.methodList = '';
        this.$axios.get('/bytespy/api/v1/listMethod',{ params: {
            jarPath:this.jarPath,
            packageName: packageName,
            className: className
        } }).then((data) => {
            if(data.code=='0'){
                var todata = [];
                data.data.forEach((item) => {
                    //  console.log(item)
                    let tmp = {}
                    tmp.type=2
                    tmp.name = item.display
                    tmp.methodName = item.name
                    tmp.packageName = item.packageName
                    tmp.params = item.params
                    tmp.signature = item.signature
                    tmp.className = item.className
                    tmp.isLeaf = true
                    todata.push(tmp);
                });
                that.methodList = todata;
                // console.log(that.methodList);
            }else{
                that.methodList=[]
            }
        }).catch(err=>{
              console.log(err)
        });
    },




    }
  
};
</script>
<style>
.tree {
  height:100% -320px;
  /* overflow: auto; */
}
.filter-tree .el-tree-node:focus > .el-tree-node__content {
  background-color: rgb(114, 192, 223) !important;
}
</style>